import {
	createApp
} from 'vue'
import App from './App.vue'
import router from './router'
import axios from 'axios'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ELIcons from '@element-plus/icons-vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'


const app = createApp(App)
app.use(router)

app.use(ElementPlus, {
	locale: zhCn
})

for (let iconName in ELIcons) {
	app.component(iconName, ELIcons[iconName])
}



axios.defaults.baseURL = '/api'; //后端开发环境地址
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'; //配置请求头信息。
app.config.globalProperties.$http = axios


// 在发送请求拦截器
axios.interceptors.request.use(config => {
	// 判断是否存在token,如果存在将每个页面header添加token
	if (sessionStorage.getItem("token")) {
		config.headers.Authorization = sessionStorage.getItem("token");
	}
	return config
})


// 添加响应拦截器
axios.interceptors.response.use(function(response) {
	if (response.data.code == '401') {
		sessionStorage.clear()
		router.replace({
			path: "/login"
		});
	}
	return response;
}, function(error) {
	Element.error("服务器错误")
	return Promise.reject(error);
});




app.mount('#app')
